package net.xdclass.coupon.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson 配置
 */
@Configuration
public class AppRedissonConfig {

    @Value("${spring.redis.host}")
    String host;
    @Value("${spring.redis.port}")
    String port;
    @Value("${spring.redis.password}")
    String pwd;

    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port)
                .setPassword(pwd)
                // 设置心跳，客户端长时间未使用，服务端会断开，throw Redis server response timeout (3000 ms)
                .setPingConnectionInterval(1000);
        /**
         * Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module @7d0b7e3c
         * 	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) ~[na:na]
         * 	at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[na:na]
         * 	at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178) ~[na:na]
         * 	at java.base/java.lang.reflect.Field.setAccessible(Field.java:172) ~[na:na]
         * 	at org.nustaq.serialization.FSTClazzInfo.createFieldInfo(FSTClazzInfo.java:503) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTClazzInfo.createFields(FSTClazzInfo.java:359) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTClazzInfo.<init>(FSTClazzInfo.java:127) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTClazzInfoRegistry.getCLInfo(FSTClazzInfoRegistry.java:129) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTClazzNameRegistry.addClassMapping(FSTClazzNameRegistry.java:98) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTClazzNameRegistry.registerClassNoLookup(FSTClazzNameRegistry.java:85) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTClazzNameRegistry.registerClass(FSTClazzNameRegistry.java:81) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTConfiguration.addDefaultClazzes(FSTConfiguration.java:799) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTConfiguration.initDefaultFstConfigurationInternal(FSTConfiguration.java:469) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTConfiguration.createDefaultConfiguration(FSTConfiguration.java:464) ~[fst-2.56.jar:na]
         * 	at org.nustaq.serialization.FSTConfiguration.createDefaultConfiguration(FSTConfiguration.java:456) ~[fst-2.56.jar:na]
         * 	at org.redisson.codec.FstCodec.<init>(FstCodec.java:47) ~[redisson-3.10.1.jar:na]
         * 	at org.redisson.config.Config.<init>(Config.java:104) ~[redisson-3.10.1.jar:na]
         * 	at org.redisson.Redisson.<init>(Redisson.java:119) ~[redisson-3.10.1.jar:na]
         * 	at org.redisson.Redisson.create(Redisson.java:164) ~[redisson-3.10.1.jar:na]
         */
        // TODO setCodec的主要目的是想不使用默认的FstCodec，里面会用到反射相关的东西，java11中抛异常，模块不可见相关的。后面在来仔细研究下这块内容
        config.setCodec(StringCodec.INSTANCE);
        return Redisson.create(config);
    }
}
